package com.esun.payment_api.controller;

import com.esun.common.api.CommonResult;
import com.esun.common.bean.PageRequest;
import com.esun.payment_api.domain.Company;
import com.esun.payment_api.domain.Department;
import com.esun.payment_api.service.LogService;
import com.esun.payment_api.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/unit")
public class UnitController {

    @Autowired
    UnitService unitService;

    @Autowired
    LogService logService;

    final String moduleName = "单位部门模块";

    @PostMapping("/getCompanyPage")
    public CommonResult getCompanyPage(@RequestBody PageRequest pageRequest){
        CommonResult commonResult = unitService.getCompanyPage(pageRequest);
        logService.writeAccessLog(moduleName, commonResult);
        return commonResult;
    }

    @PostMapping("/addCompany")
    public CommonResult addCompany(@RequestBody Company company){
        if(company.getName()==null||company.getCode()==null){
            return CommonResult.failed("公司名或公司码为空");
        }
        CommonResult commonResult = unitService.addCompany(company);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    @PostMapping("/editCompany")
    public CommonResult editCompany(@RequestBody Company company){
        if(company.getId()==null){
            return CommonResult.failed("请提供需要修改的公司的id");
        }
        CommonResult commonResult = unitService.editCompany(company);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    @PostMapping("/deleteCompany")
    public CommonResult deleteCompany(Short companyId){
        if(companyId==null){
            return CommonResult.failed("请提供需要删除的公司的id");
        }
        CommonResult commonResult = unitService.deleteCompany(companyId);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    @PostMapping("/getDepartmentPage")
    public CommonResult getDepartmentPage(@RequestBody PageRequest pageRequest){
        CommonResult commonResult = unitService.getDepartmentPage(pageRequest);
        logService.writeAccessLog(moduleName, commonResult);
        return commonResult;
    }

    @PostMapping("/addDepartment")
    public CommonResult addDepartment(@RequestBody Department department){
        if(department.getDeptName()==null||department.getCoId()==null){
            return CommonResult.failed("部门名称或部门所属公司id为空");
        }
        CommonResult commonResult = unitService.addDepartment(department);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    @PostMapping("/editDepartment")
    public CommonResult editDepartment(@RequestBody Department department){
        if(department.getId()==null){
            return CommonResult.failed("请提供需要修改的部门的id");
        }
        CommonResult commonResult = unitService.editDepartment(department);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    @PostMapping("/deleteDepartment")
    public CommonResult deleteDepartment(Short departmentId){
        if(departmentId==null){
            return CommonResult.failed("请提供需要删除的部门的id");
        }
        CommonResult commonResult = unitService.deleteDepartment(departmentId);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }


}
